﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Add_F5Plan : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;
        private string error;

        public frm_Add_F5Plan()
        {
            InitializeComponent();

            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.error = null;
            LoadPart();
            if (string.IsNullOrEmpty(error))
            {
                LoadStaff();
            }

            if (!string.IsNullOrEmpty(error))
            {
                MessageBox.Show(error);
            }
            
        }

        ////////////////////////////////////////////
        /// Load Part No List
        ////////////////////////////////////////////
        private void LoadPart()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT PartNo FROM [222_Part] ORDER BY PartNo";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["PartNo"].ToString();
                    item.Value = dataReader["PArtNo"].ToString();
                    this.partNo.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                error = "Cannot get Part list";
            }
        }

        ////////////////////////////////////////////
        /// Load Customer List
        ////////////////////////////////////////////
        private void LoadStaff()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT CustomerID, FullName FROM [222_Customer] ORDER BY FullName";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Text = dataReader["CustomerID"].ToString() + " | " + dataReader["FullName"].ToString() + ")";
                    item.Value = dataReader["CustomerID"].ToString();
                    this.customerID.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                error = "Cannot get Customer list";
            }
        }

        /////////////////////////////////////////
        /// Check Exist Order No
        ////////////////////////////////////////
        private void checkExistOrderNo()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT COUNT(ID) FROM [231_F5Plan] WHERE OrderNo='" + this.orderNo.Text + "'";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                if (dataReader.Read())
                {
                    int count = Int32.Parse(dataReader[0].ToString());
                    if (count > 0)
                    {
                        error = "The Order No has been existed in database. Please enter another Order No";
                    }
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                error = "Cannot Check Order No";
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string order = null;
            string plan = null;
            string part = null;
            int quantity = 0;
            DateTime deadline;
            string customer = null;
            string remark = null;

            ComboboxItem item = new ComboboxItem();

            if (string.IsNullOrEmpty((this.orderNo.Text)))
            {
                error = "Please enter Order No.";
            }
            else
            {
                checkExistOrderNo();
                
                if (string.IsNullOrEmpty((this.orderNo.Text)))
                {
                    order = this.orderNo.Text;
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (string.IsNullOrEmpty(this.planNo.Text))
                {
                    error = "Please enter Plan No";
                }
                else
                {
                    plan = this.planNo.Text;
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.partNo.SelectedIndex < 0)
                {
                    error = "Please select Part No";
                }
                else
                {
                    item = (ComboboxItem)this.partNo.SelectedItem;
                    part = item.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (!Int32.TryParse(this.quantityValue.Text, out quantity))
                {
                    error = "Invalid Quantity";
                }
                else
                {
                    if (quantity <= 0)
                    {
                        error = "Quantity must be greater than 0";
                    }
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.deadLine.EditValue == null)
                {
                    error = "Please select Dead Line";
                }
                else
                {
                    deadline = DateTime.Parse(this.deadLine.EditValue.ToString());
                    if (deadline <= DateTime.Now.Date)
                    {
                        error = "Dead Line must be greater than today";
                    }
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                if (this.customerID.SelectedIndex < 0)
                {
                    error = "Please select customer";
                }
                else
                {
                    item = (ComboboxItem)this.customerID.SelectedItem;
                    customer = item.Value.ToString();
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                remark = this.remarkText.Text;

                try
                {
                    deadline = DateTime.Parse(this.deadLine.EditValue.ToString());
                    myConnection.Open();
                    string sql = "INSERT INTO [231_F5Plan] (OrderNo, PlanNo, PartNo, CommandQuantity, DeadLine, CustomerID, Remark)"
                        + " VALUES ('" + order + "', '" + plan + "', '" + part + "', " + quantity + ", @deadline, '" + customer + "',"
                        + " '" + remark + "')";
                    SqlCommand cmd_sql = new SqlCommand(sql, myConnection);
                    cmd_sql.Parameters.Add("@deadline", SqlDbType.DateTime).Value = deadline.Date;
                    cmd_sql.ExecuteNonQuery();
                    cmd_sql.Dispose();
                    myConnection.Close();
                    MessageBox.Show("F5 Plan added success.");
                }
                catch //(Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    //MessageBox.Show(ex.Message);
                    MessageBox.Show("Cannot add F5 Plan. Please try again.");
                }
            }
        }
    }
}
